import json
import threading

class ConfigLoader:

    _instance_lock = threading.Lock()

    def __init__(self):
        self.weights = {}
        self.process_rates = {}
        self.prometheus_address = '127.0.0.1'
        self.prometheus_port = '9090'
        self.debug = 0
        self.duration = 40
        self.nodes = {}

    @classmethod
    def get_instance(cls, *args, **kwargs):
        if not hasattr(ConfigLoader, "_instance"):
            with ConfigLoader._instance_lock:
                if not hasattr(ConfigLoader, "_instance"):
                    ConfigLoader._instance = ConfigLoader(*args, **kwargs)
        return ConfigLoader._instance

    def loadConfig(self, path='config.json'):
        f = open(path)
        config = json.load(f)
        self.prometheus_port = config['prometheus_port']
        self.prometheus_address = config['prometheus_address']
        self.debug = config['debug']
        self.duration = float(config['duration'])
        for item in config['nodes']:
            self.nodes[item] = 1
        for item in config['target']:
            name = item['deployment_name']
            weight = float(item['weight'])
            process_rate = float(item['process_rate'])
            self.weights[name] = weight
            self.process_rates[name] = process_rate


    def updateConfig(self, path='config'):
        self.loadConfig(path)

